﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.UserSkins;
using DevExpress.XtraBars.Helpers;
using DevExpress.Skins;
using QLKhachSan.DTO;
using System.Data.SqlClient;
using QLKhachSan.DAO;
using QLKhachSan.BUS;
using DevExpress.XtraEditors;

namespace QLKhachSan.GUI
{
    public partial class fMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private fNhanVien _fNhanVien;
        private fDichVu _fDichVu = new fDichVu();
        private fPhong _fPhong = new fPhong();
        private fKhachHang _fKhachHang = new fKhachHang();
        private fDanhSachPhong _fDanhSachPhong = new fDanhSachPhong();
        private fThongBao _fThongBao = new fThongBao();
        private fThongTinPhanMen _fThongTinPhanMen = new fThongTinPhanMen();
        private fThongKe _fPhieuVaHoaDon = new fThongKe();
        private fCapNhatTaiKhoan _fCapNhatTaiKhoan = new fCapNhatTaiKhoan();
        private fInBaoCao _fInBaoCao = new fInBaoCao();
        private fBaoCao _fBaoCao ;
        private fLogin _fLogin ;
        public fMain()
        {
            InitializeComponent();
        }

        private void fMain_Load(object sender, EventArgs e)
        {            
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.XtraBars.Helpers.SkinHelper.InitSkinGallery(ribbonGalleryBarItem1, true);
            XuLyQuaHan(sender,e);
            barStaticItem1.Caption = "Bạn đang sử dụng phần mềm với quyền : " + fLogin.quyen;
            if (fLogin.quyen == "admin")
            {
                barStaticItem2.Caption = "Xin Chào Admin ";
                barButtonItem10.Enabled = true;
                navBarItemDichVu.Enabled = true;
                navBarItemKhachHang.Enabled = true;
                navBarItemNhanVien.Enabled = true;
                navBarItemPhong.Enabled = true;
            }
            else
            {
                DataTable dta_nv = DAO_NhanVien.hienthiDanhSachNhanVienTheoMaNV(int.Parse(fLogin.maNV));
                barStaticItem2.Caption = "Xin Chào " + dta_nv.Rows[0]["TenNV"].ToString();
                barButtonItem10.Enabled = true;
                navBarItemDichVu.Enabled = false; 
                navBarItemKhachHang.Enabled = false;               
                navBarItemNhanVien.Enabled = false;
                navBarItemPhong.Enabled = false ;

            }
        }
        private void XuLyQuaHan(object sender, EventArgs e)
        {
          // Xu Ly Qua Han Phieu Dat Phong
            string tenPhongPDP = "";
                    DataTable dta_ctpdp = BUS_CTPhieuDatPhong.hienthiDanhSachCTPhieuDatXuLyQuaHan();
                    DTO_Phong dto_p = new DTO_Phong();
                    for (int i = 0; i < dta_ctpdp.Rows.Count; i++ )
                    {
                        if (DateTime.Now > DateTime.Parse(dta_ctpdp.Rows[i]["NgayDen"].ToString())
                            && dta_ctpdp.Rows[i]["DaNhan"].ToString() == "Chưa" && int.Parse(dta_ctpdp.Rows[i]["MaTrangThai"].ToString()) == 2)
                        {
                            
                            dto_p.TenPhong = dta_ctpdp.Rows[i]["TenPhong"].ToString();
                            dto_p.MaTrangThai = 1;
                            tenPhongPDP += dta_ctpdp.Rows[i]["TenPhong"].ToString() + " , ";
                            BUS_Phong.suaPhongTheoTrangThai(dto_p);
                        }
                    }
                    if (tenPhongPDP.Trim() != "")
                    {
                        MessageBox.Show("Đặt Phòng Đã Quá Hạn Mà Chưa Nhận, Các Phòng : " + tenPhongPDP + "\n Sẽ Được Thay Đổi Tình Trạng Phòng (Còn Trống)", "Note !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
        
        
         }

        private void navBarItemNhanVien_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (_fNhanVien == null || _fNhanVien.IsDisposed) // Chua Tao or Chua Mo fNhanVien, Tạo Mới fNhanVien
            {
                _fNhanVien = new fNhanVien();
                _fNhanVien.MdiParent = this;
                _fNhanVien.Show();
            }
            else
            {
                _fNhanVien.Close();
                _fNhanVien = new fNhanVien();
                _fNhanVien.MdiParent = this;
                _fNhanVien.Show();
            } // da tao fNhanVien, Dong va tao moi fNhanVien
        }

        private void navBarItemDichVu_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (_fDichVu == null || _fDichVu.IsDisposed) // Chua Tao or Chua Mo fDichVu, Tạo Mới fDichVu
            {
                _fDichVu = new fDichVu();
                _fDichVu.MdiParent = this;
                _fDichVu.Show();
            }
            else
            {
                _fDichVu.Close();
                _fDichVu = new fDichVu();
                _fDichVu.MdiParent = this;
                _fDichVu.Show();
            } // da tao fDichVu, Dong va tao moi fDichVu

        }

        private void navBarItemPhong_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (_fPhong == null || _fPhong.IsDisposed) // Chua Tao or Chua Mo fPhong, Tạo Mới fPhong
            {
                _fPhong = new fPhong();
                _fPhong.MdiParent = this;
                _fPhong.Show();
            }
            else
            {
                _fPhong.Close();
                _fPhong = new fPhong();
                _fPhong.MdiParent = this;
                _fPhong.Show();
            } // da tao fPhong, Dong va tao moi fPhong

        }

        private void navBarItemKhachHang_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (_fKhachHang == null || _fKhachHang.IsDisposed) // Chua Tao or Chua Mo fKhachHang, Tạo Mới fKhachHang
            {
                _fKhachHang = new fKhachHang();
                _fKhachHang.MdiParent = this;
                _fKhachHang.Show();
            }
            else
            {
                _fKhachHang.Close();
                _fKhachHang = new fKhachHang();
                _fKhachHang.MdiParent = this;
                _fKhachHang.Show();
            } // da tao fKhachHang, Dong va tao moi fKhachHang

        }

        private void barButtonItem10_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XuLyQuaHan(sender, e);
            if (_fDanhSachPhong == null || _fDanhSachPhong.IsDisposed) // Chua Tao or Chua Mo fDanhSachPhong, Tạo Mới fDanhSachPhong
            {
                _fDanhSachPhong = new fDanhSachPhong();
                _fDanhSachPhong.MdiParent = this;
                _fDanhSachPhong.Show();
            }
            else
            {
                _fDanhSachPhong.Close();
                _fDanhSachPhong = new fDanhSachPhong();
                _fDanhSachPhong.MdiParent = this;
                _fDanhSachPhong.Show();
            } 
        }

        private void barButtonItem14_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Hide();
                _fLogin = new fLogin();                
                _fLogin.Show();
          
        }

        private void barButtonItem18_ItemClick(object sender, ItemClickEventArgs e)
        {
            XuLyQuaHan(sender, e);
            if (_fThongBao == null || _fThongBao.IsDisposed) // Chua Tao or Chua Mo fThongBao, Tạo Mới fThongBao
            {
                _fThongBao = new fThongBao();
                _fThongBao.MdiParent = this;
                _fThongBao.Show();
            }
            else
            {
                _fThongBao.Close();
                _fThongBao = new fThongBao();
                _fThongBao.MdiParent = this;
                _fThongBao.Show();
            } 
        }

        private void barButtonItem16_ItemClick(object sender, ItemClickEventArgs e)
        {            
            if (_fThongTinPhanMen == null || _fThongTinPhanMen.IsDisposed) // Chua Tao or Chua Mo fThongTinPhanMen, Tạo Mới fThongTinPhanMen
            {
                _fThongTinPhanMen = new fThongTinPhanMen();
                _fThongTinPhanMen.MdiParent = this;
                _fThongTinPhanMen.Show();
            }
            else
            {
                _fThongTinPhanMen.Close();
                _fThongTinPhanMen = new fThongTinPhanMen();
                _fThongTinPhanMen.MdiParent = this;
                _fThongTinPhanMen.Show();
            } 
        }

        private void barButtonItem15_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (_fPhieuVaHoaDon == null || _fPhieuVaHoaDon.IsDisposed) // Chua Tao or Chua Mo fPhieuVaHoaDon, Tạo Mới fPhieuVaHoaDon
            {
                _fPhieuVaHoaDon = new fThongKe();
                _fPhieuVaHoaDon.MdiParent = this;
                _fPhieuVaHoaDon.Show();
            }
            else
            {
                _fPhieuVaHoaDon.Close();
                _fPhieuVaHoaDon = new fThongKe();
                _fPhieuVaHoaDon.MdiParent = this;
                _fPhieuVaHoaDon.Show();
            } 
        }

        private void barButtonItem12_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (_fCapNhatTaiKhoan == null || _fCapNhatTaiKhoan.IsDisposed) // Chua Tao or Chua Mo fCapNhatTaiKhoan, Tạo Mới fCapNhatTaiKhoan
            {
                _fCapNhatTaiKhoan = new fCapNhatTaiKhoan();
                _fCapNhatTaiKhoan.MdiParent = this;
                _fCapNhatTaiKhoan.Show();
            }
            else
            {
                _fCapNhatTaiKhoan.Close();
                _fCapNhatTaiKhoan = new fCapNhatTaiKhoan();
                _fCapNhatTaiKhoan.MdiParent = this;
                _fCapNhatTaiKhoan.Show();
            } 
        }

        private void barButtonItem11_ItemClick(object sender, ItemClickEventArgs e)
        {
              if (_fInBaoCao == null || _fInBaoCao.IsDisposed) // Chua Tao or Chua Mo fInBaoCao, Tạo Mới fInBaoCao
            {
                _fInBaoCao = new fInBaoCao();
                _fInBaoCao.MdiParent = this;
                _fInBaoCao.Show();
            }
            else
            {
                _fInBaoCao.Close();
                _fInBaoCao = new fInBaoCao();
                _fInBaoCao.MdiParent = this;
                _fInBaoCao.Show();
            } 
        }

        private void barButtonItem13_ItemClick(object sender, ItemClickEventArgs e)
        {
            Application.Exit();
        }

        private void barButtonItem19_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (_fBaoCao == null || _fBaoCao.IsDisposed) // Chua Tao or Chua Mo fBaoCao, Tạo Mới fBaoCao
            {
                _fBaoCao = new fBaoCao();
                _fBaoCao.MdiParent = this;
                _fBaoCao.Show();
            }
            else
            {
                _fBaoCao.Close();
                _fBaoCao = new fBaoCao();
                _fBaoCao.MdiParent = this;
                _fBaoCao.Show();
            } 
        }

        private void fMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}